<?php
/**
 * Created by PhpStorm.
 * User: THINK
 * Date: 2017/8/25
 * Time: 17:47
 */
namespace Admin\Controller;
use Think\Controller;


class ArticleController extends Controller
{
     public function index(){
         $v=I('keyword')?:'';
         $id = I('pid');
         $cid = I('cid')!=I('pid')?(I('cid')?' and cid ='.I('cid'):''):'';
         $User = M('article');
         $category = M('category');
         $arr = $category->select();
         $arrs=fjd($arr,I('pid'));
         foreach ($arr as $k => $s){
             if ($s['id']==I('cid')){
                 if ($arrs[0]['id']!=$s['id']){
                 $arrs[]= $s;
                 }
             }
         }
         $this->assign('arr',jd($arr,I('cid')?:I('pid')));
         $this->assign('arrs',$arrs);
         $count = $User->where('aid=1')->count();
         $Page = new \Think\Page($count,10);
         $show  = $Page->show();
         $list = $User->alias('a')->field('a.id,a.cid,a.title,a.click,a.publishtime,a.aid,b.id as ids,b.name ')
             ->join('xyh_category b on a.cid = b.id','left')
             ->where('a.title like "%'.$v.'%" and aid=1'.$cid)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
         $this->assign('list',$list);
         $this->assign('page',$show);
         $this->assign('count',$count);
         $this->display();
     }


     public function add(){
         $User = M('article');
         $this->display();
     }


    public function upload(){
         $upload = new \Think\Upload();// 实例化上传类
         $upload->maxSize   =     3145728 ;// 设置附件上传大小
         $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
         $upload->rootPath  =      './Public/Uploads/'; // 设置附件上传目录    // 上传文件
         $info   =   $upload->upload();
              if(!$info) {
                  $this->error($upload->getError());
              }else{
                    $this->success('上传成功！');
              }
     }

    public function tp(){
         $this->display();
     }

     public function adds(){
         $User = M('article');
         $num = I('post.');
         $User->add($num);
         $this->redirect('Admin/Article/index');
     }


     public function edit(){
         $User = M('article');
         $id = I('id');
         $list = $User->find($id);
         $this->assign('list',$list);
         $this->display();
     }

     public function move(){
         $id = I('id');
         $User = M('category');
         $article = M('article');
         $list = $User->where('modelid=1')->select();
         $lists = $article->find($id);
         $this->assign('list',$list);
         $this->assign('lists',$lists);
         $this->display();

     }

     public function moves(){
         $User = M('article');
         $cid = I('cid');
         $User->where('id in('.I('id').")")->save(array('cid'=>I('cid')));
         $this->redirect('Article/index');

     }


     public function del(){

         $id=is_array(I('id'))?implode(",", I('id')):I('id');

         $data['aid']=0;
         $sun = M('article')->where('id in('.$id.')')->save($data);
         if (is_array(I('id'))){
             echo $sun > 0 ? 1 : 0;
         }else{
             if ($sun > 0) {
                 $this->success('删除成功', U('Article/index',array('id'=>I('pid'))));
             } else {
                 $this->success('删除失败', U('Article/index',array('id'=>I('pid'))));
             }
         }
     }


    public function trach(){
        $User = M('article');
        $count = $User->where('aid=0')->count();
        $list = $User->alias('a')->field('a.id,a.cid,a.title,a.click,a.publishtime,a.aid,b.id as ids,b.name ')->join('xyh_category b on a.cid = b.id','left')->where('a.title like "%'.$v.'%" and aid=0')->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);
        $this->assign('count',$count);
        $this->display();
    }


    public function haiy(){
        $id=is_array(I('id'))?implode(",", I('id')):I('id');
        $data['aid']=1;
        $sun = M('article')->where('id in('.$id.')')->save($data);
        if (is_array(I('id'))){
            echo $sun > 0 ? 1 : 0;
        }else{
            if ($sun > 0) {
                $this->success('删除成功', U('Article/index',array('id'=>I('pid'))));
            } else {
                $this->error('删除失败');
            }
        }

    }


    public function delete()
    {
        $User = M('article');
        $id = I('id');
        $id = is_array(I('id')) ? implode(',', I('id')) : I('id');
        if ($User->delete($id) !== false) {
            $this->success('删除成功', U('Article/trach',array('id'=>I('pid'))));
        } else {
            $this->error('删除失败');
        }
    }


}